package cn.iocoder.yudao.module.system.controller.admin.nodeconfig;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.nodeconfig.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.nodeconfig.NodeConfigDO;
import cn.iocoder.yudao.module.system.service.nodeconfig.NodeConfigService;

@Tag(name = "管理后台 - 审核节点")
@RestController
@RequestMapping("/system/node-config")
@Validated
public class NodeConfigController {

    @Resource
    private NodeConfigService nodeConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建审核节点")
    public CommonResult<Integer> createNodeConfig(@Valid @RequestBody NodeConfigSaveReqVO createReqVO) {
        return success(nodeConfigService.createNodeConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新审核节点")
    public CommonResult<Boolean> updateNodeConfig(@Valid @RequestBody NodeConfigSaveReqVO updateReqVO) {
        nodeConfigService.updateNodeConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除审核节点")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteNodeConfig(@RequestParam("id") Integer id) {
        nodeConfigService.deleteNodeConfig(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除审核节点")
    public CommonResult<Boolean> deleteNodeConfigList(@RequestParam("ids") List<Integer> ids) {
        nodeConfigService.deleteNodeConfigListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得审核节点")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<NodeConfigRespVO> getNodeConfig(@RequestParam("id") Integer id) {
        NodeConfigDO nodeConfig = nodeConfigService.getNodeConfig(id);
        return success(BeanUtils.toBean(nodeConfig, NodeConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得审核节点分页")
    public CommonResult<PageResult<NodeConfigRespVO>> getNodeConfigPage(@Valid NodeConfigPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<NodeConfigDO> pageResult = nodeConfigService.getNodeConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, NodeConfigRespVO.class));
    }



    @GetMapping("/export-excel")
    @Operation(summary = "导出审核节点 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportNodeConfigExcel(@Valid NodeConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<NodeConfigDO> list = nodeConfigService.getNodeConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "审核节点.xls", "数据", NodeConfigRespVO.class,
                        BeanUtils.toBean(list, NodeConfigRespVO.class));
    }

}